<?php

namespace privateClass\App;

class Config {
	/**
	 * 获取当前query_string的配置节内容
	 * @param unknown $_query_string
	 * @param unknown $_app_settings
	 * @throws \Exception
	 * @return Ambigous <boolean, \privateClass\App\multitype:, multitype:, unknown>
	 */
	public static function get($_query_string, $_app_settings, $_is_static = FALSE) {
		$result = FALSE;
		$_conf = \privateClass\Kernel\INI::read($_app_settings['config_path']);
		
		//初始化config节配置
		$_conf['config'] = self::parse_config_node($_conf['config']);
		
		if (
			//判断app_id是否对应
			($_app_settings['app_id'] != $_conf['config']['app_id']) || ($_app_settings['app_code'] != $_conf['config']['app_code'])
			||
			//应用认证
			\privateClass\Auth\Auther::getInstance()->get_auth($_app_settings['app_id'], $_app_settings['app_code'], $_conf['config']['access_secret_key']) == FALSE
			) throw new \Exception();
		else {
			//初始化matching_rules节配置
			//2012-12-07算法不需要进行所有rules节的配置,只需针对当前query_string进行配置
			//self::parse_matching_rules_node($_conf['matching_rules']);
			
			$result = array_merge($_conf['config'],['config'=>self::current_rule($_query_string, $_conf['matching_rules'])]);
		}
		return $result;
	}
	/**
	 * 获取当前配置节点
	 * @param unknown $_matching_rules
	 */
	private static function current_rule($_query_string, $_matching_rules) {
		$result = FALSE;
		$_match = FALSE;
		if ($_query_string['query_uri'] != '') {
			foreach ($_matching_rules as $key => $rule) {
				if (isset($rule['query']['settings']['method']) && defined('_'.$rule['query']['settings']['method'])) ;
				else $rule['query']['settings']['method'] = 'HTTP_GET';
				if (preg_match($rule['uri'], $_query_string['query_uri']) && constant('_'.$rule['query']['settings']['method']) == $_query_string['method']) {
					$_match = $key;
					break;
				}
			}
		} else {
			reset($_matching_rules);
			$_match = key($_matching_rules);
		}
		return self::init_rule($_matching_rules[$_match]);
	}
	
	/**
	 * 处理config节配置
	 * 若无app_id,app_code配置,则为空
	 * 默认tmplate_extension = .html
	 * 默认default_uri = FALSE
	 * @param $_config
	 */
	private static function parse_config_node ($_config) {
		isset($_config['app_id']) ? TRUE : $_config['app_id'] = '';
		isset($_config['app_code']) ? TRUE : $_config['app_code'] = '';
		isset($_config['access_secret_key']) ? TRUE : $_config['access_secret_key'] = '';
		isset($_config['template_extension']) ? TRUE : $_config['template_extension'] = '.html';
		isset($_config['default_uri']) ? TRUE : $_config['default_uri'] = FALSE;
		return $_config;
	}
	/**
	 * 初始化配置子节点内容
	 * @param unknown $rule
	 * @return multitype:
	 */
	private static function init_rule ($rule) {
		$result = $rule;
		//basic settings
		/*$rule['index']*/
		$result['index'] = !isset($rule['index']) ? _PROJECT_DEFAULT_INDEX_FILE : $rule['index'];
		/*$rule['data_type']*/
		$result['data_type'] = !isset($rule['data_type']) ? 'html' : $rule['data_type'];
		/*$rule['template']*/
		$result['template'] = !isset($rule['template']) ? 'no-template.html' : $rule['template'];
		//query settings
		/*$rule['query']['settings']*/
		/*$rule['query']['settings']['method']*/
		$result['query']['settings']['method'] = !isset($rule['query']['settings']['method']) ? 'HTTP_GET' : $rule['query']['settings']['method'];
		/*$rule['query']['settings']['enctype']*/
		$result['query']['settings']['enctype'] = (!isset($rule['query']['settings']['enctype']) || empty($rule['query']['settings']['enctype'])) ? (isset($_SERVER['HTTP_CONTENT_TYPE']) ? $_SERVER['HTTP_CONTENT_TYPE'] : '') : $rule['query']['settings']['enctype'];
		/*$rule['query']['settings']['follow_location']*/
		$result['query']['settings']['follow_location'] = (isset($rule['query']['settings']['follow_location']) && $rule['query']['settings']['follow_location'] != FALSE) ? TRUE : FALSE;
		/*$rule['query']['settings']['timeout']*/
		$result['query']['settings']['timeout'] = isset($rule['query']['settings']['timeout']) ? $rule['query']['settings']['timeout'] : 10;
		/*$rule['query']['settings']['enable_default_cookie']*/
		$result['query']['settings']['enable_default_cookie'] = isset($rule['query']['settings']['enable_default_cookie']) ? $rule['query']['settings']['enable_default_cookie'] : TRUE;
		/*$rule['query']['settings']['use_raw_post_string']*/
		$result['query']['settings']['use_raw_post_string'] = (isset($rule['query']['settings']['use_raw_post_string']) && $rule['query']['settings']['use_raw_post_string'] != FALSE) ? TRUE : FALSE;
		/*$rule['query']['custom']['referer']*/
		$result['query']['custom']['referer'] = isset($rule['query']['custom']['referer']) ? $rule['query']['custom']['referer'] : 'http://air.wo-app.com';
		/*$rule[['query']['custom']['user_agent']*/
		$result['query']['custom']['user_agent'] = isset($rule['query']['custom']['user_agent']) ? $rule['query']['custom']['user_agent'] : 'Air Mobile Service 1.0;';
		/*$rule[['query']['custom']['cookie']*/
		$result['query']['custom']['cookie'] = isset($rule['query']['custom']['cookie']) ? $rule['query']['custom']['cookie'] : [];
		/*$rule[['query']['custom']['header']*/
		$result['query']['custom']['header'] = isset($rule['query']['custom']['header']) ? $rule['query']['custom']['header'] : [];
		/*$rule[['query']['custom']['content_type']*/
		$result['query']['custom']['content_type'] = isset($rule['query']['custom']['content_type']) ? $rule['query']['custom']['content_type'] : 'text/plain';
		/*$rule[['query']['custom']['charset']*/
		$result['query']['custom']['charset'] = isset($rule['query']['custom']['charset']) ? $rule['query']['custom']['charset'] : 'utf-8';
		
		
		$result['vars'] = isset($rule['vars']) ? $rule['vars'] : [];
		return $result;
	}
	
	private static function parse_matching_rules_node (&$_matching_rules) {
		foreach ($_matching_rules as $i => $rule) {
			$_matching_rules[$i] = self::init_rule($rule);	
		}
	}
}

?>